package com.cg.controller.business;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.Announcement;
import com.cg.entity.BusinessApplication;
import com.cg.service.impl.AnnouncementServiceImpl;
import com.cg.service.impl.BusinessApplicationServiceImpl;
import com.cg.utils.Constant;
import com.cg.utils.DateUtils;
import com.cg.utils.JpushUtil;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.annotation.SysLog;

/**
 * 待办业务
 * @author cumt
 *
 */
@RestController
@RequestMapping("/sys/application")
public class BusinessApplicationController extends AbstractController{
	
	@Autowired
	BusinessApplicationServiceImpl businessApplicationServiceImpl;
	@Autowired
	AnnouncementServiceImpl announcementServiceImpl;
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:application:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		
		//查询列表数据
		Query query = new Query(params);
//		
//		BusinessApplication application=new BusinessApplication();
		List<BusinessApplication> businessList = businessApplicationServiceImpl.queryList(query);
		int total = businessApplicationServiceImpl.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", businessList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:application:info")
	public R info(@PathVariable("id") Long id){
		BusinessApplication application = businessApplicationServiceImpl.queryObject(id);
		
		
		return R.ok().put("application", application);
	}
	
	
	/**
	 * 保存用户
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("sys:application:save")
	public R save(@RequestBody BusinessApplication application){
		application.setApplicationTime(DateUtils.dateToDateString(new Date()));
		businessApplicationServiceImpl.save(application);
		
		return R.ok();
	}
	/**
	 * 修改用户
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("sys:application:update")
	public R update(@RequestBody BusinessApplication application){
		
//		comment.set(getUserId());
		if(application.getExamineFlg()==1) {
			application.setExamineTime(DateUtils.dateToDateString(new Date()));
		}
		businessApplicationServiceImpl.update(application);
		JpushUtil.sendPushToSomeOne(application.getPhone(), application.getExamineText(), "回执信息");
		Announcement announcement=new Announcement();
		announcement.setAnnContent(application.getExamineText());
		announcement.setAnnName("回执信息");
		announcement.setPushUser(application.getPhone());
		announcement.setType("2");
		announcement.setCreateTime(DateUtils.dateToDateString(new Date()));
//		if(getUserId()) {
//			
//		}
		announcement.setPublishUser(Integer.parseInt(String.valueOf(getUserId())));
		announcementServiceImpl.save(announcement);
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:application:delete")
	public R delete(@RequestBody Long[] ids){
		
		businessApplicationServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
	
}
